package com.samsung.sds.ssa.jsca.editor;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.contentassist.IContextInformationValidator;

public class MyCompletionProcessor implements IContentAssistProcessor {

	@Override
	public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
		return computeCompletionProposals(viewer.getDocument(), offset);

		/*
		 * List<String> list = new ArrayList<String>(); ICompletionProposal[]
		 * result = new ICompletionProposal[list.size()];
		 * 
		 * for (int i = 0; i < list.size(); ++i) { result[i] = new
		 * CompletionProposal(list.get(i), offset, 0, list.get(i).length()); }
		 * 
		 * return result;
		 */
	}

	private ICompletionProposal[] computeCompletionProposals(IDocument document, int offset) {
		ICompletionProposal[] result = null;

		try {
			String candidate = getCurrentCandidate(document, offset);
			result = JSExplorer.computeCompletionProposals(candidate, offset);

			if (candidate.length() > 0) {

				// final ISpellCheckEngine engine =
				// SpellCheckEngine.getInstance();
				// final ISpellChecker checker =
				// engine.createSpellChecker(engine.getLocale(),
				// PreferenceConstants.getPreferenceStore());
				//
				// if (checker != null) {
				//
				// final ArrayList proposals = new
				// ArrayList(checker.getProposals(candidate,
				// Character.isUpperCase(candidate.charAt(0))));
				// result = new ICompletionProposal[proposals.size()];
				//
				// RankedWordProposal word = null;
				// for (int proposal = 0; proposal < result.length; proposal++)
				// {
				//
				// word = (RankedWordProposal) proposals.get(proposal);
				// if (word.getText().startsWith(candidate))
				// word.setRank(word.getRank() + PREFIX_RANK_SHIFT);
				// }
				//
				// // Collections.sort(proposals);
				//
				// String text = null;
				// for (int proposal = 0; proposal < result.length; proposal++)
				// {
				//
				// word = (RankedWordProposal) proposals.get(proposal);
				// text = word.getText();
				//
				// // result[result.length - proposal - 1]= new
				// // JavaCompletionProposal(text, start,
				// // candidate.length(),
				// //
				// JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_RENAME),
				// // text, 0);
				// result[proposal] = new JavaCompletionProposal(text, start,
				// candidate.length(),
				// JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_RENAME),
				// text, word.getRank());
				// }
				// }
			}
		} catch (BadLocationException exception) {
			// fError = (result == null) ? exception.getLocalizedMessage() :
			// null;
		}

		return result;
	}

	@SuppressWarnings("unused")
	private String getCurrentCandidate(IDocument document, int offset) throws BadLocationException {
		ITypedRegion region = document.getPartition(offset);
		String content = document.get(region.getOffset(), region.getLength());
		int index = offset - region.getOffset() - 1;
		boolean isInParentheses = false;
		int parenthesesCount = 0;

		while (index >= 0) {
			char c = content.charAt(index);

			if (c == ';' || ((c == ',' || c == '}' || c == '=' || c == '+' || c == '-' || c == '*' || c == '/') && !isInParentheses)) {
				break;
			}

			if (c == ')') {
				isInParentheses = true;
				++parenthesesCount;
			} else if (c == '(') {
				if (--parenthesesCount == 0) {
					isInParentheses = false;
				}
			}

			index--;
		}

		final int start = region.getOffset() + index + 1;
		String candidate = content.substring(index + 1, offset - region.getOffset());

		candidate = candidate.replaceAll("\\n", "").replaceAll("\\t", "").replaceAll("\\r", "").trim();

		return candidate;
	}

	@Override
	public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
		return null;
	}

	@Override
	public char[] getCompletionProposalAutoActivationCharacters() {
		return new char[]{'.'};
	}

	@Override
	public char[] getContextInformationAutoActivationCharacters() {
		return null;
	}

	@Override
	public String getErrorMessage() {
		return null;
	}

	@Override
	public IContextInformationValidator getContextInformationValidator() {
		return null;
	}
}
